package com.union.core.information.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.information.model.Announcement;
import com.union.core.information.service.AnnouncementService;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;

public class AnnouncementAction extends ActionSupport{
	
	private static final long serialVersionUID = 872316012305159L;
	private SysService sysService = SysService.getInstance();
	private static Logger log = Logger.getLogger(AnnouncementAction.class.getName());
	
	private AnnouncementService announcementService;
	private List<Announcement> announcementList;
	private Announcement announcement;
	
	//公告信息
	private int announcement_id;          //公告id    
	private String announcement_name;     //公告名称
	private String announcement_content;  //公告内容
	private String start_date;            //起始日期
	private String end_date;              //结束日期
	private String add_user;              //添加人   
	private String add_time;              //添加日期
	private String update_user;           //修改人   
	private String update_time;           //修改日期
	private String join_date1;
	private String join_date2;
	private int org_id;
	//private String receive;
	
	//分页
	private int start;
	private int intPageSize =15; //每页行数
	private int page = 1;      //页码
	private int intRowCount;   //总行数
	private int intPageCount;  //总页数
	private int intPage;       //页数变量
	private String paramName;
	private String paramValue;
	private String orderByName;
	//日志
	private SysLogsService logsSer;
	//权限控制
	private List<String> allPower;

	

	public AnnouncementAction(){
		announcementService = new AnnouncementService();
		announcementList = new ArrayList<Announcement>();
		announcement = new Announcement();
		logsSer = new SysLogsService();
	}

	public String queryList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
				allPower=sysService.getSessionListener().getAllPower();
				add_user = sysService.getSessionListener().getMANAGER_NAME();
				lookup();
				setQuerySession();
				return "SUCCESS";
		}	
	}
	
	public String queryList1(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
				allPower=sysService.getSessionListener().getAllPower();
				org_id = sysService.getSessionListener().getPERSONNEL().getOrganization_id();
				lookup();
				setQuerySession();
				return "SUCCESS";
		}	
	}
	
	/**
	 * 查询列表(多页)
	 * @return String
	 */
	public String pagelist(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			getQuerySession();  //get the query sessions before lookup!
			lookup();
			return "SUCCESS";
		}
	}
	
	/*
	 * 加载添加公告
	 */
	public String addload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			return "SUCCESS";
		}
	}
	
	/*
	 * 添加公告
	 */
	public String add(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			announcement.setAnnouncement_name(announcement_name);
			announcement.setAnnouncement_content(announcement_content);
			announcement.setStart_date(start_date);
			announcement.setEnd_date(end_date);
			announcement.setAdd_user(sysService.getSessionListener().getMANAGER_NAME());
			System.out.println(announcement.getReceive()+"AAAAAAAAAAAAA");
			if(announcement.getReceive()!=null){
				announcement.setReceive(","+announcement.getReceive()+",");
			}
			
			announcementService.add(announcement);
			logsSer.addSysLogs("添加公告【"+announcement_name+"】");
			return "SUCCESS";
		}
	}


	/*
	 * 加载修改公告
	 */
	public String editload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			announcement = announcementService.query(announcement_id);
			return "SUCCESS";
		}
	}
	
	/*
	 * 查询公告信息
	 */
	public String query(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			announcement = announcementService.query(announcement_id);
			return "SUCCESS";
		}
	}
	
	/*
	 * 修改公告信息
	 */
	public String edit(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			announcement.setAnnouncement_name(announcement_name);
			announcement.setAnnouncement_content(announcement_content);
			announcement.setStart_date(start_date);
			announcement.setEnd_date(end_date);
			announcement.setUpdate_user(sysService.getSessionListener().getMANAGER_NAME());
			
			announcementService.edit(announcement);
			logsSer.addSysLogs("编辑公告【"+announcement_name+"】");

			return "SUCCESS";
		}
	}
	
	/*
	 * 删除公告信息
	 */
	public String delete(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			announcement = announcementService.query(announcement_id);
			announcementService.delete(announcement_id);
			logsSer.addSysLogs("编辑公告【"+announcement.getAnnouncement_name()+"】");
			return "SUCCESS";
		}
	}
	
	private void lookup(){
		
		if("".equals(announcement_id))announcement_id = 0;
		if("".equals(announcement_name))announcement_name = null;
		if("".equals(announcement_content))announcement_content = null;
		if("".equals(join_date1))join_date1 = null;
		if("".equals(join_date2))join_date2 = null;
		if("".equals(add_user))add_user = null;
		if("".equals(add_time))add_time = null;
		
		if("".equals(paramName))paramName = null;
		if("".equals(paramValue))paramValue = null;
		if("".equals(orderByName))orderByName = null;
		
		if(page<1){
			page = 1;
			intPage = 1;
		}else{
			intPage = page;
		}
		intRowCount = announcementService.quertListCount(announcement_id, announcement_name, announcement_content, 
				join_date1, join_date2, add_user, add_time, org_id, paramName, paramValue);
		intPageCount = (intRowCount + intPageSize -1)/intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			announcementList = announcementService.queryList(announcement_id, announcement_name, announcement_content, 
					join_date1, join_date2, add_user, add_time,org_id, paramName, paramValue, orderByName, start, intPageSize);
	    }
	}
	
	private void setQuerySession(){
		ServletActionContext.getRequest().getSession().setAttribute("announcement_id",announcement_id); 
		ServletActionContext.getRequest().getSession().setAttribute("announcement_name",announcement_name); 
		ServletActionContext.getRequest().getSession().setAttribute("announcement_content",announcement_content); 
		ServletActionContext.getRequest().getSession().setAttribute("join_date1",join_date1); 
		ServletActionContext.getRequest().getSession().setAttribute("join_date2",join_date2); 
		ServletActionContext.getRequest().getSession().setAttribute("add_user",add_user); 
		ServletActionContext.getRequest().getSession().setAttribute("add_time",add_time); 
		ServletActionContext.getRequest().getSession().setAttribute("paramName",paramName); 
		ServletActionContext.getRequest().getSession().setAttribute("paramValue",paramValue); 
		ServletActionContext.getRequest().getSession().setAttribute("orderByName",orderByName); 
		ServletActionContext.getRequest().getSession().setAttribute("org_id",org_id); 
	}
	
	private void getQuerySession(){
		announcement_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("announcement_id");
		announcement_name = (String)ServletActionContext.getRequest().getSession().getAttribute("announcement_name");
		announcement_content = (String)ServletActionContext.getRequest().getSession().getAttribute("announcement_content");
		join_date1 = (String)ServletActionContext.getRequest().getSession().getAttribute("join_date1");
		join_date2 = (String)ServletActionContext.getRequest().getSession().getAttribute("join_date2");
		add_user = (String)ServletActionContext.getRequest().getSession().getAttribute("add_user");
		add_time = (String)ServletActionContext.getRequest().getSession().getAttribute("add_time");
		paramName = (String)ServletActionContext.getRequest().getSession().getAttribute("paramName");
		paramValue = (String)ServletActionContext.getRequest().getSession().getAttribute("paramValue");
		orderByName = (String)ServletActionContext.getRequest().getSession().getAttribute("orderByName");
		org_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("org_id");
	}
	public SysService getSysService() {
		return sysService;
	}

	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	public List<Announcement> getAnnouncementList() {
		return announcementList;
	}

	public void setAnnouncementList(List<Announcement> announcementList) {
		this.announcementList = announcementList;
	}

	public Announcement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	public int getAnnouncement_id() {
		return announcement_id;
	}

	public void setAnnouncement_id(int announcementId) {
		announcement_id = announcementId;
	}

	public String getAnnouncement_name() {
		return announcement_name;
	}

	public void setAnnouncement_name(String announcementName) {
		announcement_name = announcementName;
	}

	public String getAnnouncement_content() {
		return announcement_content;
	}

	public void setAnnouncement_content(String announcementContent) {
		announcement_content = announcementContent;
	}

	public String getStart_date() {
		return start_date;
	}

	public void setStart_date(String startDate) {
		start_date = startDate;
	}

	public String getEnd_date() {
		return end_date;
	}

	public void setEnd_date(String endDate) {
		end_date = endDate;
	}

	public String getAdd_user() {
		return add_user;
	}

	public void setAdd_user(String addUser) {
		add_user = addUser;
	}

	public String getAdd_time() {
		return add_time;
	}

	public void setAdd_time(String addTime) {
		add_time = addTime;
	}

	public String getUpdate_user() {
		return update_user;
	}

	public void setUpdate_user(String updateUser) {
		update_user = updateUser;
	}

	public String getUpdate_time() {
		return update_time;
	}

	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getIntPageSize() {
		return intPageSize;
	}

	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getIntRowCount() {
		return intRowCount;
	}

	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}

	public int getIntPageCount() {
		return intPageCount;
	}

	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public String getParamName() {
		return paramName;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	public String getParamValue() {
		return paramValue;
	}

	public void setParamValue(String paramValue) {
		this.paramValue = paramValue;
	}

	public String getOrderByName() {
		return orderByName;
	}

	public void setOrderByName(String orderByName) {
		this.orderByName = orderByName;
	}

	public String getJoin_date1() {
		return join_date1;
	}

	public void setJoin_date1(String joinDate1) {
		join_date1 = joinDate1;
	}

	public String getJoin_date2() {
		return join_date2;
	}

	public void setJoin_date2(String joinDate2) {
		join_date2 = joinDate2;
	}
	public List<String> getAllPower() {
		return allPower;
	}

	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}

	public int getOrg_id() {
		return org_id;
	}

	public void setOrg_id(int orgId) {
		org_id = orgId;
	}

}